from django.db import models
import datetime
from decimal import Decimal
from StartGroup.models import Group
from django.forms import ModelForm
from django import forms
from datetime import date
from utils.widgets import DatePicker

TITLE_CHOICES = (
    ('MR', 'Mr.'),
    ('MRS', 'Mrs.'),
    ('MS', 'Ms.'),
)
        
class Category( models.Model ):
    name = models.CharField( max_length = 200, default = 'Unspecified', db_index=True )
    isActive = models.BooleanField(default=1)
    parents = models.ManyToManyField('self', symmetrical=False, blank=True, null=True)
    
    def __unicode__(self):
        return self.name

class Transaction( models.Model  ):
    group = models.ForeignKey(Group, unique=False)
    date = models.DateField( 'Transaction Date', db_index=True )
    description = models.CharField( max_length = 200, default = 'Unspecified' )
    #category = models.ForeignKey( Category )
    category = models.CharField(max_length = 70, default = 'Unspecified', blank = True)
    amount = models.DecimalField(decimal_places=2, max_digits = 11, null=True, blank=True)
    comment = models.CharField( max_length = 200, null=True, blank=True )
    
    #def __init__(self):
    #    super(Transaction, self).__init__(*args, **kwargs )

     
class TransactionForm(forms.Form):
    
    description = forms.CharField(max_length=150, required=True, label='Description', error_messages={'required': 'Please enter Description'},
                                widget=forms.TextInput(attrs={'size':'50', 'id':'funding_desc_id'}))
    
    comment = forms.CharField(max_length=350, required=False, label='Comments',
                                widget=forms.Textarea(attrs={'size':'350', 'cols':'40', 'rows':'5', 'id':'comments_id'}))

    amount = forms.CharField(required=True, label='Amount', error_messages={'required':'Please enter amount'},
                                 widget=forms.TextInput(attrs={'size':'15', 'id':'funding_amount_id'}))
    
    date = forms.DateField(required=True, label='Date', error_messages={'required': 'Please enter date'},
                                 widget=DatePicker())
    
    category = forms.CharField( max_length=70, label = 'Category', widget= forms.Select(choices = TITLE_CHOICES) )
                
    
    def save(self, group_info):
        new_transaction = Transaction( group = group_info,
                            date=self.cleaned_data['date'],
                           comment=self.cleaned_data['comment'],
                           amount=self.cleaned_data['amount'],
                           description=self.cleaned_data['description'],
                           category = self.cleaned_data['category'])
        new_transaction.save()
        return new_transaction
    
    
def CategoryForm( excludeList, *args, **kwargs ):
    class MyCategoryForm( ModelForm ):
        class Meta:
            model = Category
            exclude = excludeList
            
            def __init__( self ):
                super( MyCategoryForm, self ).__init__( *args, **kwargs )
    
    return MyCategoryForm();